﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Wicresoft.SelfService.DAO;
using Wicresoft.SelfService.BL;
using Wicresoft.SelfService.BL.Interface;
using Wicresoft.SelfService.DAL;

namespace Wicresoft.SelfService.Portal.Controllers
{
    public class ServiceMaintainController : BaseController
    {
        private string ARServer;
        private string Mid_Base;
        private string appSystemName;
        public ITServiceHelper helper { get; private set; }
        public IUserSync userSync { get; private set; }
        public Wicresoft.SelfService.BL.Interface.IAppSystem myAppSystem { get; private set; }
        public static WFService.ApproveFlowClient IApproveFlow { get; private set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (helper == null) { helper = new ITServiceHelper(); }
            if (userSync == null) { userSync = new UserSync(); }
            if (myAppSystem == null) { myAppSystem = new MyAppSystem(); }
            if (IApproveFlow == null) { IApproveFlow = new WFService.ApproveFlowClient(); }

            ARServer = ConfigurationManager.AppSettings["ARServer"];
            Mid_Base = ConfigurationManager.AppSettings["ITSM_Mid_Tier"];//without end "/"
            appSystemName = ConfigurationManager.AppSettings["ApplicationSystemName"];
            if (string.IsNullOrEmpty(appSystemName)) appSystemName = "应用系统";
            base.Initialize(requestContext);
        }
        //
        // GET: /ServiceMaintain/

        [Authorize]
        public ActionResult ClassifyMaintain()
        {
            List<ClassifyConfig> model = helper.getClassifyInfo();

            return View(model);

        }

        [Authorize]
        public ActionResult ShowClassify(int ID)
        {
            ClassifyConfig model = new ClassifyConfig();
            model = helper.getClassifyByID(ID);

            var TierModelList = helper.getServiceEventTypeList();
            var myTier1 = (from p in TierModelList select p.Tier1).Distinct();
            ViewData["Tier1"] = new SelectList(myTier1.ToList());
            //ViewData["Tier1"] = myTier1;

            var myTier2 = (from p in TierModelList where p.Tier1 == model.Applvl1 select p.Tier2).Distinct();
            ViewData["Tier2"] = new SelectList(myTier2.ToList());
                //myTier2;

            var myTier3 = (from p in TierModelList where p.Tier1 == model.Applvl1 && p.Tier2 == model.Applvl2 select p.Tier3).Distinct();
            ViewData["Tier3"] = new SelectList(myTier3.ToList()); 
                //myTier3;

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult UpdateClassify(ClassifyConfig model)
        {
            bool isOk = helper.updateClassify(model);
            return Content(isOk ? "true" : "false");
        }



        [HttpPost]
        public ActionResult DeleteClassify(ClassifyConfig classify) 
        {
            bool isOk = helper.deleteClassify(classify);
            return Content(isOk ? "true" : "false");
        }

        public ActionResult CreateClassify() 
        {
            var model = helper.getServiceEventTypeList();
            var mylist = (from p in model select p.Tier1).Distinct();
            ViewData["Tier1"] = mylist;
            return View();
        }

        [HttpPost]
        public ActionResult CreateClassify(ClassifyConfig model)
        {
            model.Needapprove = Convert.ToInt32(Request.Form["NeedApprove"]);
            model.Isevent = Convert.ToInt32(Request.Form["IsEvent"]);
            model.Applvl1 = Request.Form["Applvl1"];
            model.Applvl2 = Request.Form["Applvl2"];
            model.Applvl3 = Request.Form["Applvl3"];
            int sid = Convert.ToInt32(Request.Form["ServiceID"]);
            //public enum AppLvl1
            //{
            //    桌面终端 = 1,网络接入 = 2,安全防病毒 = 3,应用系统 = 4
            //}

            switch (sid)
            {
                case 1: 
                    model.ServiceID = 1;
                    model.PartialName = "Desktop";
                    break;
                case 2: 
                    model.ServiceID = 9;
                    model.PartialName = "NetworkAccess";
                    break;
                case 3: 
                    model.ServiceID = 14;
                    model.PartialName = "Antivirus";
                    break;
                case 4: 
                    model.ServiceID = 17;
                    model.PartialName = "AppSystem";
                    break;
                case 5:
                    model.ServiceID = 23;
                    model.PartialName = "Business";
                    break;
            }
            //if (string.IsNullOrEmpty(model.Applvl1))
            //{
            //    model.Applvl1 = model.ServiceName;
            //}
            //if (string.IsNullOrEmpty(model.Applvl2))
            //{
            //    model.Applvl2 = model.ServiceName;
            //}
            //if (string.IsNullOrEmpty(model.Applvl3))
            //{
            //    model.Applvl3 = model.ServiceName;
            //}

            bool isOk = helper.createClassify(model);
            return Content(isOk ? "true" : "false");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult GetTier2List(string tier1)
        {
            var model = helper.getServiceEventTypeList();
            var mylist = (from p in model where p.Tier1==tier1 select p.Tier2 ).Distinct();
            return Json(mylist, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult GetTier3List(string tier1, string tier2)
        {
            var model = helper.getServiceEventTypeList();
            var mylist = (from p in model where p.Tier1 == tier1 && p.Tier2 == tier2 select p.Tier3).Distinct();
            return Json(mylist, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult SearchByServiceName(ClassifyConfig model)
        {
            model.ServiceName = Request.Form["ServiceName"];
            int sid = Convert.ToInt32(Request.Form["ServiceID"]);

            //public enum AppLvl1
            //{
            //    桌面终端 = 0,网络接入 = 1,安全防病毒 = 2,应用系统 = 3
            //}

            switch (sid)
            {
                case 1:
                    model.ServiceID = 1;
                    break;
                case 2:
                    model.ServiceID = 9;
                    break;
                case 3:
                    model.ServiceID = 14;
                    break;
                case 4:
                    model.ServiceID = 17;
                    break;
                case 5:
                    model.ServiceID = 23;
                    break;
            }

            bool flag = helper.searchByServiceName(model);

            return Content(flag ? "true" : "false");
        
        }


        public string createCodeNumber()
        {
            return helper.getServiceRecordNo();
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (this.helper != null)
                helper.Dispose();

            if (userSync != null && userSync is UserSync)
            {
                ((UserSync)userSync).Dispose();
            }
        }

    }
}
